package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;
//1. 角色列表查询
    @GetMapping("/roles")
    public Result listRoles(String name, String label,
    @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        //System.out.println("查询部门：" + name + " " + status + " " + page + " " + pageSize);
        log.info("分页查询部门：name={},label={},page={},pageSize={}", name, label, page, pageSize);
        PageResult<Role> pageResult = roleService.selectlist(name, label, page, pageSize);
        return Result.success(pageResult);
    }
    //    2. 根据ID查询
    @GetMapping("/roles/{id}")
    public Result getById(@PathVariable Integer id) {
        System.out.println("查询角色：" + id);
        Role role = roleService.getById(id);
        return Result.success(role);

    }

    @Log
    //3. 添加角色
    @PostMapping("/roles")
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色：{}", role);
        roleService.addRole(role);
        return Result.success();
    }
    @Log
    //4、删除角色
    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色：{}", id);
        roleService.delete(id);
        return Result.success();
    }

    //5. 修改角色
    @Log
    @PutMapping("/roles")
    public Result update(@RequestBody Role role) {
        log.info("修改角色：{}", role);
        roleService.update(role);
        return Result.success();
    }
   //6、 查询所有角色
    @GetMapping("/roles/list")
    public Result list() {
        log.info("查询所有角色");
        return Result.success(roleService.getAllRoles());
    }








}
